# -*- coding: utf-8 -*-
# @Author: slp
# @Date:   2017-04-05 23:45:14
# @Last Modified by:   slp
# @Last Modified time: 2017-09-27 09:24:27

def format_size(cs):
	c = ["b", "kb", "mb", "gb"]
	rs = ""
	for i in xrange(3, -1, -1):
		# if cs[i]>0:
		rs += "%d%s"%(cs[i], c[i])
	return rs


def addr2size(addr):
	rs = [0,0,0,0]
	s = addr
	for i in xrange(4):
		m = s % 1024
		rs[i] = m
		s = s / 1024
	return rs

def size2addr(size):
	size = size.upper()
	cs = ["G", "M", "K"]
	km = {"B":0}
	for c in  cs:
		km[c] = 0
	size = size.strip("B").split("B")
	for i in xrange(len(size)):
		match = False
		for c in cs:
			if size[i].endswith(c):
				km[c] = int(size[i].rstrip(c))
				match = True
				break
		if i == len(size)-1 and not match:
			km["B"] = int(size[i])
		else:
			continue
	cs.append("B")
	mci = [1024*1024*1024, 1024*1024, 1024, 1]
	addr = 0
	for i in xrange(len(cs)):
		addr += km[cs[i]] * mci[i]
	return addr


addrs = [
	0x13000000,
	0x131fffff,
	0x80000000,
	0x13090000,
	0x2230000,
	0x2240000,
	0x2c000000,
	0x2C011000,
	0x2C010000,
	0x10440000,
	0x10003b8,
	0xc000000,
	0xa808571600000000,
	0x16570978
]



for a in addrs:
	rs = addr2size(a)
	print "%#x: %s"%(a, format_size(rs))


sizes = [
	"1GB33MB1KB",
	"0gb304mb576kb0b",
	"0gb192mb64kb0b",
	"512kb"
]

for s in sizes:
	rs = size2addr(s)
	print "%s : %#x"%(s, rs)


def m_bin(n):
	s = bin(n)
	s = list(s)
	p=1
	for i in xrange(len(s), 3, -4):
		s.insert(i-4, '_')
		i -= 2
	return "".join(s)

nums = [0x0000000001002222]

for n in nums:
	print m_bin(n)




print bin(0xd)
print bin(0x5)
print bin(0xc)
print bin(0x800003cd)

print m_bin(0x0010000d)

